package com.softeem.controller;


import com.softeem.domain.Comment;
import com.softeem.domain.User;
import com.softeem.domain.vo.CommentVO;
import com.softeem.mapper.CommentMapper;
import com.softeem.mapper.UserMapper;
import com.softeem.service.ICommentService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-07-01
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    ICommentService service;
    @Autowired
    CommentMapper mapper;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/findAllComment")
    public String findAllComment(@ModelAttribute("traId")Integer traId, Model model){

        List<CommentVO> allComment = service.findAllComment();
        List<CommentVO> comList = new ArrayList<>();

        for (CommentVO com : allComment) {
            if (com.getTraId()==traId){
                comList.add(com);
            }
        }

        model.addAttribute("comList",comList);

        return "/comment-all";
     }

   @RequestMapping("/addComment")
   public String addComment(@CookieValue("uId")Integer uId, @ModelAttribute("traId")Integer traId, HttpServletRequest req, Model model,String pContent){
       System.out.println(uId);
       System.out.println(traId);

//       String queryString = req.getQueryString();
//       Integer traid = Integer.valueOf(queryString);
//       System.out.println(traid);

       Comment comment = new Comment();
       comment.setTraId(traId);
       comment.setpContent(pContent);
       comment.setuId(uId);


       mapper.insert(comment);

       findAllComment(traId,model);

       return "/comment-all";
    }


}

